/**
 * 检索结果页面(i18n, m版共用)
 * @author tanggf
 */
$(function(){
	search_index.run();
});

var search_index = {
		run: function(){
			this._init_();
			this._start_();
			this._init_events();			
		},
		
		_init_: function(){		
			this.searchType = $("#queryType").val() || 'all';		
			this.query = $("#query").val();	
			//搜索结果数
			this.searchCountMap = {};
			//当前访问的页数
			this.searchPageMap = {};
			this.highlightReg = new RegExp("(" + this.query + ")", 'gi');			
			this.pageSize = 30;
			
			var path = location.pathname;
			if(path.startWith(main.contextPath + "/world/")){
				this.isI18n = true;
			}				
		},
		
		_start_: function(){					
			$("#search_result_ul > li > a").each(function(){
				var $this = $(this);
				var type = $this.attr("searchType");
				var count = $this.attr("count");			
				var iCount = parseInt(count.replace(/[,]/g, ''));
				search_index.searchCountMap[type] = iCount;
				search_index.searchPageMap[type] = 0;
				if(search_index.searchType == type){
					$this.addClass("curr");
				}
				
				var $div = $("#" + type + "_search_div");
				var rg = " Results for <strong>\"" + search_index.query + "\"</strong>";
				if(iCount == 0){
					$div.children("div.search_column").html("No" + rg);
					$div.children("div.search_cont").html($("#no_result_div").html());
				}else{
					$div.children("div.search_column").html(count + rg);
				}
			});					
			
			this._handle_query_videos();
		},		
		
		_init_events: function(){
			$("#searchBox_cont").smenu({
				defaultValue : 'Search for Videos...',
				onchange : function(query) {					
					query = encodeURIComponent(query);
					if (query) {
						if(search_index.isI18n){
							location.href = main.contextPath + "/world/results?search=" + query;
						}else{
							location.href = main.contextPath + "/results?search=" + query;
						}						
					}
				}
			});
			
			$("#search_result_ul > li > a").click(function(){		
				var $this = $(this);
				if($this.data("clicking")){
					return false;
				}
				$this.data("clicking", true);
				var type = $this.attr("searchType");		
				$("#search_result_ul > li > a").removeClass("curr");
				$this.addClass("curr");				
				
				if(type != search_index.searchType){
					search_index.searchType = type;
					search_index._handle_query_videos();	
				}		
				
				$this.data("clicking", false);
			});		
			
			//scroll event
			$.scroll(function(){
				search_index._handle_scroll();
			});			
			
			if(this.isI18n){
				$("#gotoTop").easeup();
			}
		},
				
		_handle_query_videos: function(){
			$("#main_search > div").hide();
			$("#" + this.searchType + "_search_div").show();	
			var page = this.searchPageMap[this.searchType];			
			if( page == 0){
				this._query_videos();
			}			
		},
		
		_query_videos: function(page){					
			var $loading = $("#" + this.searchType + "_search_div > div.search_cont > div.loadingBox");
			if($loading.data("querying")){
				return;
			}
			
			$loading.data("querying", true);
			$loading.show();
			
			var $w = $(window);	
			page = page || 1;			
			var url = main.contextPath + "/video/query";
			if(this.isI18n){
				url = main.contextPath + "/world/video/query";
			}
			var data = this._gather_params(page);
			$.get(url, data, function(result){
				result = search_index._highlight_fly_result(result);				
				$loading.before(result).hide();
				search_index.searchPageMap[search_index.searchType] = page;
				if(page == 1){
					//考虑大屏的滚动
					if($w.height() >= $(document).height()){
						search_index._query_videos(2);
					}				
				}
			}).complete(function(){
				$w.data("scrolling", false);			
				$loading.data("querying", false);			
			});			
		},				
		
		_gather_params: function(page){
			return ['page=', page ,'&searchType=', this.searchType, '&query=', encodeURIComponent(this.query)].join("");
		},
		
		_highlight_fly_result: function(result){
			var $result = $("<div>" + result + "</div>");						
			$("div.search-box > ul > li.name > a", $result).each(function(){
				var $this = $(this);
				var old = $this.text();		
				if(old.length > 40){
					old = old.substring(0, 40) + "...";
				}
				$this.html(old.replace(search_index.highlightReg, "<span class=\"red\">$1</span>"));
			});		
			
			$("div.search-box > div.img", $result).hover(function() {
				$(this).children('div.tc_btn').fadeIn("slow");
			}, function() {
				$(this).children('div.tc_btn').stop(true, false).fadeOut("fast");
			});
			
			return $result.children();
		},
		
		_handle_scroll: function(){			
			if( ! this._has_more_result()){
				return;
			}
			var $w = $(window);
			var scrolling = $w.data("scrolling");
			if(scrolling){
				return;
			}
			
			$w.data("scrolling", true);		
			var page = this.searchPageMap[this.searchType];
			this._query_videos(page + 1);								
		},
		
		_has_more_result: function(){
			var count = this.searchCountMap[this.searchType];		
			if(count == 0){
				return false;
			}
			
			var page = this.searchPageMap[this.searchType];
			var pageCount = Math.ceil(count / this.pageSize);			
			if(pageCount < 2 || page == pageCount){
				return false;
			}					
			
			return true;
		}
};